<template>
  <div>
    <el-row style="padding: 20px 0 0 20px">
      <el-col :span="12" style="margin: 0 10px 0 0">
        <el-input v-model="project.name" placeholder="请输入项目名称">
          <template slot="prepend">项目名</template>
        </el-input>
      </el-col>
    </el-row>
    <el-row style="padding: 20px 0 0 20px">
      <el-col :span="10" style="margin: 0 10px 0 0">
        <el-input v-model="project.github" placeholder="请输入github链接">
          <template slot="prepend">GitHub</template>
        </el-input>
      </el-col>
      <el-col :span="10" style="margin: 0 10px 0 0">
        <el-input v-model="project.gitee" placeholder="请输入gitee链接">
          <template slot="prepend">码云</template>
        </el-input>
      </el-col>
    </el-row>
    <el-row style="padding: 20px 0 0 20px">
      <el-col :span="10" style="margin: 0 10px 0 0">
        <el-input v-model="project.Url" placeholder="请输入链接地址">
          <template slot="prepend">展示链接</template>
        </el-input>
      </el-col>
      <el-col :span="10" style="margin: 0 10px 0 0">
        <el-input v-model="project.language" placeholder="请输入项目编写语言">
          <template slot="prepend">编写语言</template>
        </el-input>
      </el-col>
    </el-row>
    <el-row style="padding: 20px 0 0 20px; display: flex!important;flex-direction: column !important;">
      <el-col :span="2">
        <span style="color:#888;padding: 20px 0;">项目简介</span>
      </el-col>
      <el-col :span="12" style="margin: 0 10px 0 0">
        <el-input
          v-model="project.discription"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          placeholder="请输入简介内容"
          resize="none"
          maxlength="200"
        />
      </el-col>
    </el-row>
    <el-row style="padding: 20px 0 0 20px">
      <el-col :span="6">
        <Upload :img-url="project.thumb" @upload="imgUrlUp" />
      </el-col>
    </el-row>
    <el-row style="padding: 20px 0 0 40px">
      <el-col :span="6">
        <el-button type="primary" @click="submit">提交</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Upload from '@/components/uploade'
import { Post_requset } from '@/api/publicRequest'
import { apiAddProject, apiUpProject } from '@/routerConfig'
import { Message } from 'element-ui'
export default {
  components: {
    Upload
  },
  data() {
    return {
      project: {
        _id: '',
        name: '',
        github: '',
        gitee: '',
        discription: '',
        thumb: '',
        Url: '',
        language: ''
      },
      tempPrject: {},
      isUp: false
    }
  },
  created() {
    const query = this.$route.query
    if (query._id) {
      // console.log('up', this.$route.query)
      // this.project = { ...this.$route.query }
      // console.log()
      for (const p in this.project) {
        this.project[p] = query[p]
      }
      this.tempPrject = { ...this.project }
    }
  },
  methods: {
    imgUrlUp(e) {
      this.project.thumb = e
    },
    async submit() {
      try {
        const str = this.project._id === '' ? '添加' : '修改'
        console.log(this.project._id)
        let result = null
        if (this.project._id) {
          for (const p in this.tempPrject) {
            if (this.tempPrject[p] !== this.project[p]) {
              this.isUp = true
              break
            }
          }
          if (!this.isUp) {
            Message.success('无需提交修改')
            return
          }
          result = await Post_requset(apiUpProject, this.project)
        } else {
          result = await Post_requset(apiAddProject, this.project)
        }

        if (typeof result.data === 'string') {
          Message.error(result.data)
        } else {
          Message.success(`${str}成功`)
          this.$router.push({
            name: 'projectList'
          })
        }
      } catch (e) {
        Message.error(e)
      }
    }
  }
}
</script>

<style>

</style>
